<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程订单回收站管理
 */
class Orderrecycle extends Backend
{
    // 当前对象模型
    protected $model = null;

    // 设置关联查询
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Order');
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    
    // 还原
    public function reduction($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if(!$row)
        {
            $this->error('课程订单不存在');
        }

        $result = $this->model->onlyTrashed()->where('id', 'IN', $ids)->update(['deletetime' => null]);


        if(!$result)
        {
            $this->error($this->model->getError());
        }

        $this->success();
    }

    // 删除
    public function del($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if(!$row)
        {
            $this->error('课程订单不存在');
        }

        $result = $this->model->destroy($ids, true);

        if(!$result)
        {
            $this->error($this->model->getError());
        }
        
        $this->success();
    }
}
